import Context from "../Context";
import { Middleware } from "../Compose";

const bodyParse: Middleware<Context> = async (ctx, next) => {
  if (typeof ctx.req.body === "string") {
    ctx.req.body = JSON.parse(ctx.req.body);
  }

  await next();

  if (
    ctx.res.response != null &&
    typeof ctx.res.response.code !== "undefined"
  ) {
    ctx.res.status = ctx.res.response.code;
  }
};

export default bodyParse;
